<?php
/**
 * Created by PhpStorm.
 * User: huawei
 * Date: 2021/11/3
 * Time: 21:07
 */

namespace app\system\admin;
use app\common\model\Access;
use think\Db;

class Datas extends Admin
{
    public $areas=[
        "ASuu0myA"=>"集团",
        "11hMO8ja"=>"湾中",
        "KIiBvEqV"=>"轨交",
        "Vb9rv47X"=>"湾东",
        "kIJkusb8"=>"湾西",
        "guN7mouM"=>"华中",
        "1aHTPZOK"=>"华东",
        "TsnYW0Kb"=>"北方",
        "IrOaUOpG"=>"西南"
    ];
    protected function initialize()
    {
        parent::initialize();
        set_time_limit(0);
    }
    public function index(){
        $isdown = $this->request->get("isdown");
        $this->assign("h5url",url('index/index/index','',false,true));
        $Access = new Access();
        //越秀集团数据总数
        $ASuu0myACount =$Access->countSql("organizationCode='ASuu0myA'")[0]["num"];
        $this->assign("ASuu0myACount",$ASuu0myACount);
        //越秀集团数据openid去重
        $ASuu0myAGroupbyuserid =count($Access->groupSql("organizationCode='ASuu0myA'","userid"));
        $this->assign("ASuu0myAGroupbyuserid",$ASuu0myAGroupbyuserid);

        //越秀集团数据openid去重查看留电人数
        $ASuu0myAPhone =count($Access->groupSql("organizationCode='ASuu0myA' AND phone is not null","userid"));
        $this->assign("ASuu0myAPhone",$ASuu0myAPhone);

        //越秀集团数据openid去重查看新增人数
        $ASuu0myANew =count($Access->groupSql("organizationCode='ASuu0myA' AND newStatus =1","userid"));
        $this->assign("ASuu0myANew",$ASuu0myANew);


        //越秀湾中数据总数
        $a11hMO8jaCount =$Access->countSql("organizationCode='11hMO8ja'")[0]["num"];
        $this->assign("a11hMO8jaCount",$a11hMO8jaCount);

        //越秀湾中数据openid去重
        $a11hMO8jaGroupbyuserid =count($Access->groupSql("organizationCode='11hMO8ja'","userid"));
        $this->assign("a11hMO8jaGroupbyuserid",$a11hMO8jaGroupbyuserid);

        //越秀湾中数据openid去重查看留电人数
        $a11hMO8jaPhone =count($Access->groupSql("organizationCode='11hMO8ja' AND phone is not null","userid"));
        $this->assign("a11hMO8jaPhone",$a11hMO8jaPhone);

        //越秀湾中数据openid去重查看新增人数
        $a11hMO8jaNew =count($Access->groupSql("organizationCode='11hMO8ja' AND newStatus =1","userid"));
        $this->assign("a11hMO8jaNew",$a11hMO8jaNew);

        //越秀轨交数据总数
        $KIiBvEqVCount =$Access->countSql("organizationCode='KIiBvEqV'")[0]["num"];
        $this->assign("KIiBvEqVCount",$KIiBvEqVCount);

        //越秀轨交数据openid去重
        $KIiBvEqVGroupbyuserid =count($Access->groupSql("organizationCode='KIiBvEqV'","userid"));
        $this->assign("KIiBvEqVGroupbyuserid",$KIiBvEqVGroupbyuserid);

        //越秀轨交数据openid去重查看留电人数
        $KIiBvEqVPhone =count($Access->groupSql("organizationCode='KIiBvEqV' AND phone is not null","userid"));
        $this->assign("KIiBvEqVPhone",$KIiBvEqVPhone);

        //越秀轨交数据openid去重查看新增人数
        $KIiBvEqVNew =count($Access->groupSql("organizationCode='KIiBvEqV' AND newStatus =1","userid"));
        $this->assign("KIiBvEqVNew",$KIiBvEqVNew);

        //越秀湾东数据总数
        $Vb9rv47XCount =$Access->countSql("organizationCode='Vb9rv47X'")[0]["num"];
        $this->assign("Vb9rv47XCount",$Vb9rv47XCount);

        //越秀湾东数据openid去重
        $Vb9rv47XGroupbyuserid =count($Access->groupSql("organizationCode='Vb9rv47X'","userid"));
        $this->assign("Vb9rv47XGroupbyuserid",$Vb9rv47XGroupbyuserid);

        //越秀湾东数据openid去重查看留电人数
        $Vb9rv47XPhone =count($Access->groupSql("organizationCode='Vb9rv47X' AND phone is not null","userid"));
        $this->assign("Vb9rv47XPhone",$Vb9rv47XPhone);

        //越秀湾东数据openid去重查看新增人数
        $Vb9rv47XNew =count($Access->groupSql("organizationCode='Vb9rv47X' AND newStatus =1","userid"));
        $this->assign("Vb9rv47XNew",$Vb9rv47XNew);

        //越秀湾西数据总数
        $kIJkusb8Count =$Access->countSql("organizationCode='kIJkusb8'")[0]["num"];
        $this->assign("kIJkusb8Count",$kIJkusb8Count);

        //越秀湾西数据openid去重
        $kIJkusb8Groupbyuserid =count($Access->groupSql("organizationCode='kIJkusb8'","userid"));
        $this->assign("kIJkusb8Groupbyuserid",$kIJkusb8Groupbyuserid);

        //越秀湾西数据openid去重查看留电人数
        $kIJkusb8Phone =count($Access->groupSql("organizationCode='kIJkusb8' AND phone is not null","userid"));
        $this->assign("kIJkusb8Phone",$kIJkusb8Phone);

        //越秀湾西数据openid去重查看新增人数
        $kIJkusb8New =count($Access->groupSql("organizationCode='kIJkusb8' AND newStatus =1","userid"));
        $this->assign("kIJkusb8New",$kIJkusb8New);

        //越秀华中数据总数
        $guN7mouMCount =$Access->countSql("organizationCode='guN7mouM'")[0]["num"];
        $this->assign("guN7mouMCount",$guN7mouMCount);

        //越秀华中数据openid去重
        $guN7mouMGroupbyuserid =count($Access->groupSql("organizationCode='guN7mouM'","userid"));
        $this->assign("guN7mouMGroupbyuserid",$guN7mouMGroupbyuserid);

        //越秀华中数据openid去重查看留电人数
        $guN7mouMPhone =count($Access->groupSql("organizationCode='guN7mouM' AND phone is not null","userid"));
        $this->assign("guN7mouMPhone",$guN7mouMPhone);

        //越秀华中数据openid去重查看新增人数
        $guN7mouMNew =count($Access->groupSql("organizationCode='guN7mouM' AND newStatus =1","userid"));
        $this->assign("guN7mouMNew",$guN7mouMNew);


        //越秀华东数据总数
        $a1aHTPZOKCount =$Access->countSql("organizationCode='1aHTPZOK'")[0]["num"];
        $this->assign("a1aHTPZOKCount",$a1aHTPZOKCount);

        //越秀华东数据openid去重
        $a1aHTPZOKGroupbyuserid =count($Access->groupSql("organizationCode='1aHTPZOK'","userid"));
        $this->assign("a1aHTPZOKGroupbyuserid",$a1aHTPZOKGroupbyuserid);

        //越秀华东数据openid去重查看留电人数
        $a1aHTPZOKPhone =count($Access->groupSql("organizationCode='1aHTPZOK' AND phone is not null","userid"));
        $this->assign("a1aHTPZOKPhone",$a1aHTPZOKPhone);

        //越秀华东数据openid去重查看新增人数
        $a1aHTPZOKNew =count($Access->groupSql("organizationCode='1aHTPZOK' AND newStatus =1","userid"));
        $this->assign("a1aHTPZOKNew",$a1aHTPZOKNew);


        //越秀北方数据总数
        $TsnYW0KbCount =$Access->countSql("organizationCode='TsnYW0Kb'")[0]["num"];
        $this->assign("TsnYW0KbCount",$TsnYW0KbCount);

        //越秀北方数据openid去重
        $TsnYW0KbGroupbyuserid =count($Access->groupSql("organizationCode='TsnYW0Kb'","userid"));
        $this->assign("TsnYW0KbGroupbyuserid",$TsnYW0KbGroupbyuserid);

        //越秀北方数据openid去重查看留电人数
        $TsnYW0KbPhone =count($Access->groupSql("organizationCode='TsnYW0Kb' AND phone is not null","userid"));
        $this->assign("TsnYW0KbPhone",$TsnYW0KbPhone);

        //越秀北方数据openid去重查看新增人数
        $TsnYW0KbNew =count($Access->groupSql("organizationCode='TsnYW0Kb' AND newStatus =1","userid"));
        $this->assign("TsnYW0KbNew",$TsnYW0KbNew);

        //越秀西南数据总数
        $IrOaUOpGCount =$Access->countSql("organizationCode='IrOaUOpG'")[0]["num"];
        $this->assign("IrOaUOpGCount",$IrOaUOpGCount);

        //越秀西南数据openid去重
        $IrOaUOpGGroupbyuserid =count($Access->groupSql("organizationCode='IrOaUOpG'","userid"));
        $this->assign("IrOaUOpGGroupbyuserid",$IrOaUOpGGroupbyuserid);

        //越秀西南数据openid去重查看留电人数
        $IrOaUOpGPhone =count($Access->groupSql("organizationCode='IrOaUOpG' AND phone is not null","userid"));
        $this->assign("IrOaUOpGPhone",$IrOaUOpGPhone);

        //越秀西南数据openid去重查看新增人数
        $IrOaUOpGNew =count($Access->groupSql("organizationCode='IrOaUOpG' AND newStatus =1","userid"));
        $this->assign("IrOaUOpGNew",$IrOaUOpGNew);
        return $this->fetch();

    }

    /**
     * @title 数据来源
     * @return string
     */
    public function source(){

        $datas = [];
        $Access = new Access();
        foreach ($this->areas as $key=>$val){
            //总访问人数
            $groupbyuserid =count($Access->groupSql("organizationCode='{$key}'","userid"));
            //客户分享
            $v1=count($Access->groupSql("organizationCode='{$key}' AND visitorStatus=0","userid"));
            //销售分享
            $v2=count($Access->groupSql("organizationCode='{$key}' AND visitorStatus = 1","userid"));
            //其他
            $other = count($Access->groupSql("organizationCode='{$key}' AND visitorStatus is null","userid"));
            $datas[]=[
                "name"=>$val,
                "num"=>$groupbyuserid,
                "v1"=>$v1,
                "v2"=>$v2,
                "other"=>$other
            ];
        }
        $this->assign("datas",$datas);
        return $this->fetch();
    }

    /**
     * @title 海报分享数据
     * @return string
     */
    public function sharedata(){
        $Access = new Access();
        $poster=[];
        $poster['poster1']["cnum"] = Db::name("qrcode")->where('typec',1)->sum("cnum");
        $poster['poster1']['count1'] =count($Access->groupSql("poster=1"));
        $poster['poster1']['count2'] =count($Access->groupSql("poster=1",'userid'));
        $poster['poster1']['count3'] =count($Access->groupSql("poster=1 AND phone is not null",'userid'));

        $poster['poster2']["cnum"] = Db::name("qrcode")->where('typec',2)->sum("cnum");
        $poster['poster2']['count1'] =count($Access->groupSql("poster=2"));
        $poster['poster2']['count2'] =count($Access->groupSql("poster=2",'userid'));
        $poster['poster2']['count3'] =count($Access->groupSql("poster=2 AND phone is not null",'userid'));


        $poster['poster3']["cnum"] = Db::name("qrcode")->where('typec',3)->sum("cnum");
        $poster['poster3']['count1'] =count($Access->groupSql("poster=3"));
        $poster['poster3']['count2'] =count($Access->groupSql("poster=3",'userid'));
        $poster['poster3']['count3'] =count($Access->groupSql("poster=3 AND phone is not null",'userid'));

        $this->assign("datas",$poster);

        return $this->fetch();
    }

    /**
     * @title 卡券领取详情
     * @return string
     * @throws \think\exception\DbException
     */
    public function gitflist(){
        $lists = Db::name('gift_list')
            ->field("u.phone,u.nickname,g.*")
            ->alias("g")
            ->leftJoin("userinfo u",'g.uid=u.id')
            ->paginate();
        $this->assign("lists",$lists);
        return $this->fetch();
    }

    /**
     * @title 红包统计接口
     * @return string
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function redsum(){
        $redconfig = Db::name('red_rate')->where(1)->select();
        foreach ($redconfig as &$val){
            $count = Db::name('red_list')->where("rid",$val["id"])->count("id");
            $val["count"]=$count;
            $num = Db::name('red_list')
                ->where("rid",$val["id"])
                ->group("uid")
                ->count("id");
            $val["num"]=$num;
        }
        unset($val);
        $this->assign("lists",$redconfig);
        return $this->fetch();
    }

    public function redlog(){
        $lists = Db::name('red_list')
            ->field("r.*,u.nickname,u.phone")
            ->alias("r")
            ->leftJoin("yuexiu_userinfo u",'u.id = r.uid')
            ->paginate();
        $this->assign("areas",$this->areas);
        $this->assign("lists",$lists);
        return $this->fetch();
    }
}